package lily.com.app;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.StrikethroughSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ListItemOnClickListener implements View.OnClickListener{
	
	private int tid = -1;
	TaskListViewAdapter adapter = null;
	
	//TODO refactor it later
	public ListItemOnClickListener () {}

	@Override
	public void onClick(View v) {
//		Log.d("DEBUG", "ListItemOnClickListener onClick()");
		int vid = v.getId();
		switch(vid){
		case R.id.txtRecord:
			txtRecordOnClick(v);
			break;
		case R.id.btnFinish:
			btnFinishOnClick(v);
			break;
		case R.id.btnDelete:
			btnDeleteOnClick(v);
			break;
		default:
			break;	
		}
		
	}
	
	private void btnDeleteOnClick(View v) {
//		Log.d("DEBUG", "btnDeleteOnClick()");
		
		View row = (View)v.getParent();
		tid = row.getId();
		if(null == adapter)
		{
			TaskListView listView = (TaskListView)row.getParent();
			adapter = (TaskListViewAdapter)listView.getAdapter();
		}
		
		AlertDialog dialog = new AlertDialog.Builder(((TaskListView)row.getParent()).getContext())
        .setTitle("删除？")
        .setPositiveButton("删除", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
        		adapter.deleteTask(tid);
        		if(adapter.getCount() == 0){
        			adapter.insertItem(0);
        		}
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {}
        })
        .create();
		dialog.show();
	}

	private void btnFinishOnClick(View v) {
//		Log.d("DEBUG", "btnFinishOnClick()");
		
		View row = (View)v.getParent();
		int tid = row.getId();
		
		if(null == adapter)
		{
			TaskListView listView = (TaskListView)row.getParent();
			adapter = (TaskListViewAdapter)listView.getAdapter();
		}
		
		// add stroke-through effect
		TextView txtRecord = (TextView)row.findViewById(R.id.txtRecord);
		//TODO bug to fix
		String title = txtRecord.getText().toString();
//		Log.d("DEBUG", "txtRecord.getText() = " + title);
		SpannableString ss = new SpannableString(title);
		ss.setSpan(new StrikethroughSpan(), 0,
				title.length(),
				Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
		txtRecord.setText(ss);
		adapter.updateTaskStatus(tid, TaskDomain.TaskStatus.DONE, TaskDomain.TaskUIStatus.NONE);
		Button btnFinish = (Button) row.findViewById(R.id.btnFinish);
		btnFinish.setVisibility(View.GONE);
	}

	public void txtRecordOnClick(View v){
//		Log.d("DEBUG", "txtRecordOnClick()");
		
		View row = (View)v.getParent();
		
		if(null == adapter)
		{
			TaskListView listView = (TaskListView)row.getParent();
			adapter = (TaskListViewAdapter)listView.getAdapter();
		}
		
		Intent i = new Intent("lily.com.app.EDIT");
		Bundle bundle = new Bundle();
		bundle.putInt(TaskDao.TaskDaoFields.COL_NAME_TID, row.getId());
		i.putExtras(bundle);
		row.getContext().startActivity(i);
	}

}
